/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.me

import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_edit_gender.*
import kotlinx.android.synthetic.main.include_header.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.ApiManager
import top.stime21.myneighbours.base.BuzBaseActivity
import top.stime21.myneighbours.base.io_main
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.utils.ACache

class EditGenderActivity : BuzBaseActivity() {

    private lateinit var user: User

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_gender)
        tv_title.setText("选择性别")
        btn_back.setOnClickListener { finish() }
        female.setOnClickListener(l)
        male.setOnClickListener(l)
        user = ACache.get(this).getAsObject("user") as User
    }

    private val l: View.OnClickListener = object : View.OnClickListener {
        override fun onClick(view: View?) {
            when (view!!.getId()) {
                R.id.male -> updateGender(0)
                R.id.female -> updateGender(1)
            }
        }
    }

    fun updateGender(gender: Int) {
        showLoadding()
        ApiManager.ins().dataApi.updateInfo("gender", gender.toString() + "", user.userId).io_main()
                .subscribe({ r ->
                    if (r.data == null) return@subscribe
                    ACache.get(this@EditGenderActivity).put("user", r.data)
                    hideLodding()
                    finish()
                }, { t ->
                    hideLodding()
                    showToast("请求失败")
                })
    }
}
